﻿using Microsoft.AspNetCore.Mvc;

namespace SimpleTicketBooking.Controllers.Base
{
    /// <summary>
    /// 
    /// </summary>
    public class APIControllerBase : ControllerBase
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        /// <exception cref="UnauthorizedAccessException"></exception>
        protected int GetUserId()
        {

            if (!HttpContext.Items.TryGetValue("Id", out var userIdObj) || userIdObj == null)
            {
                throw new UnauthorizedAccessException("Unauthorized");
            }

            if (!int.TryParse(userIdObj.ToString(), out var userId))
            {
                throw new UnauthorizedAccessException("Unauthorized");
            }

            return userId;
        }
    }
}
